package com.fucheng.server;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 设计服务注册中心
 */
public class ServerRegistry {

    // key为serviceName（支持版本号拼接），value为服务配置
    private static final Map<String, ServiceConfig<?>> serviceMap = new ConcurrentHashMap<>();

    // 注册服务
    public static <T> void register(ServiceConfig<T> serviceConfig) {
        String serviceKey = buildServiceKey(serviceConfig.getServiceName(), serviceConfig.getVersion());
        serviceMap.put(serviceKey, serviceConfig);
    }

    // 获取服务
    public static ServiceConfig<?> getService(String serviceName, String version) {
        String serviceKey = buildServiceKey(serviceName, version);
        return serviceMap.get(serviceKey);
    }

    // 拼接服务唯一标识符：接口名 + 版本号
    private static String buildServiceKey(String serviceName, String version) {
        return serviceName + ":" + version;
    }
}
